package com.example.program.service.impl;

import com.example.program.mapper.EmpMapper;
import com.example.program.pojo.Emp;
import com.example.program.pojo.PageBean;
import com.example.program.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        PageHelper.startPage(page,pageSize);

        //2、获取分页查询结果列表
        List<Emp> empList = empMapper.page(name,gender,begin,end);
        Page<Emp> p= (Page<Emp>) empList;
        //3、封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal() , p.getResult());
        return pageBean;
    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {

emp.setCreateTime(LocalDateTime.now());
emp.setUpdateTime(LocalDateTime.now());
empMapper.insert(emp);
    }

    @Override
    public Emp getByid(Integer id) {
        //返回emp 查询
        Emp emp=empMapper.getByid(id);

        return emp;
    }

    @Override
    public void update(Emp emp) {
        // 修改emp
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp loginEmp = empMapper.getByUsernameAndPassword(emp);
        return  loginEmp;
    }
}
